/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.api.entityinterface.resourceinfo;

import com.inspur.edp.bef.bizentity.GspBizEntityElement;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bff.spi.builtin.BffPropertyInfo;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementDataType;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.InitColumnsGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.initcolumnsstragety.ExtendAddPropertyInfoProcessor;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.GspViewModelElement;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import lombok.var;
import org.eclipse.jdt.core.dom.Expression;

import java.util.ArrayList;

public class BffInitColumnsGenerator extends InitColumnsGenerator {
    private  DataTypeInterfaceGeneratorContext context;
    protected BffInitColumnsGenerator(IGspCommonDataType dataType, DataTypeInterfaceGeneratorContext parentContext) {
        super(dataType, parentContext);
        this.context=parentContext;
    }
    public  boolean generateChildElemnts() {
        return false;
    }
    @Override
    protected Class getDataTypePropertyInfoType() {
        return BffPropertyInfo.class;
    }

    @Override
    protected void addExtendArgs(IGspCommonField field, ArrayList<Expression> args) {
        args.add(ast.newBooleanLiteral(field.isBigNumber()));
        boolean isvirtual=field.getIsVirtual();
        if(field instanceof GspViewModelElement) {

            if(!isvirtual) {
                GspViewModelElement element = (GspViewModelElement) field;
                String beMetaDataID = element.getMapping().getTargetMetadataId();
                String beElementID = element.getMapping().getTargetElementId();

                GspMetadata metadata = this.context.getApiGenContext().getCefGenContext()
                    .getMetadataJitContext().getMetadata(beMetaDataID);
                GspBusinessEntity beEntity = (GspBusinessEntity) metadata.getContent();

                GspBizEntityElement beElement = (GspBizEntityElement) beEntity
                    .findElementById(beElementID);
                String beLabelId = beElement.getLabelID();
                var arg12 = ExpressionUtils.getStringLiteral(ast, beLabelId);
                args.add(arg12);
            } else {
                args.add(ast.newNullLiteral());
            }

            GspViewModel viewModel=(GspViewModel) (((GspViewModelElement) field).getBelongObject().getBelongModel());

            if(field.getObjectType()== GspElementObjectType.Association ||field.getIsUdt()||field.getMDataType()== GspElementDataType.DateTime) {

                args.add(ast.newBooleanLiteral(viewModel.getEnableStdTimeFormat()));
            } else {
                args.add(ast.newBooleanLiteral(false));
            }
        }

    }

    @Override
    protected ExtendAddPropertyInfoProcessor getExtendAddPropertyInfoProcessor() {
        return new BffExtendAddPropertyInfoProcessor();
    }

    protected boolean useNewAddPropertyInfoMethod() {
        return true;
    }
}
